<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>真实的终端</title>

    <link rel="icon" type="image/x-icon" href="/static/favicon.ico">
    <link rel="stylesheet" href="/static/css/global.css">

    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap-theme.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">

    <script src="https://cdn.bootcss.com/jquery/3.4.1/core.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.js"></script>

    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/npm.js"></script>

    <script src="https://cdn.bootcss.com/socket.io/2.3.0/socket.io.js"></script>
</head>
<body>
<iframe src="/play_start" style="height: 99vh; width: 99vw; border: none" id="terminal"></iframe>

<script>
    const socket = io();

    let device_name = prompt('请输入设备名称', 'test device for {{username}}');

    socket.on('connect', function () {
        socket.emit('first_connection', {'device name': device_name,});
        document.title = '【真终端】成功连接服务器';
    });

    socket.on('content upload', function (msg) {
        console.log(msg);
        let json_data = JSON.parse(msg);
        console.log(json_data);
        let time = json_data['time'];
        let id = json_data['id'];
        let title = json_data['title'];
        console.log(id);
        console.log(time);
        document.title = '【真终端】成功更新内容';
        $('#terminal').attr('src', '/play_start?id=' + id + '&time=' + time + '&title=' + title);
    });

    socket.on('disconnect', function () {
        document.title = '【真终端】失去服务器连接！';
    });

    window.onbeforeunload = function () {
        socket.disconnect();
    };
</script>
</body>
</html>